package com.fifth.emp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Message;
import com.fifth.emp.config.WebUtils;
import com.fifth.emp.mapper.MessageMapper;
import com.fifth.emp.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 系统通知表 服务实现类
 * </p>
 *
 * @author 徐志豪
 * @since 2022-09-02
 */
@Service
public class MessageServiceImpl implements IMessageService {


    @Autowired
    private MessageMapper messageMapper;

    /**
     * 发送消息通知
     */
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public CommonResult sendMessage(Message message) {
        //获取userid
        HttpServletRequest request = WebUtils.getRequest();
        //获取token
        String token = request.getHeader("token");
        //根据token获取userid
        Map userMap = JSON.parseObject(redisTemplate.opsForValue().get(token), Map.class);
        String userid;
        if (userMap!=null && userMap.containsKey("user")) {
            JSONObject user = (JSONObject) userMap.get("user");
            userid = user.getString("id");
        }else {
            return new CommonResult(4000,"登录失效,请登录后再发布",null);
        }

        message.setEid(userid);

//        Role role = messageMapper.selectRName(userid);
//        message.setEid(role.getRoleName());
//        System.out.println("===="+role.getRoleName());
//        System.out.println(message.getEid());
        int insert = messageMapper.insert(message);
        return new CommonResult(2000,"发送通知成功",insert);
    }

    /**
     * 时间分页查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public CommonResult getMessage(Integer currentPage, Integer pageSize, String startTime,String endTime ) {

        IPage<Message> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("e.is_deleted",0);
        //,@Param("starttime") String starttime,@Param("endtime") String endtime
        if(StringUtils.hasText(startTime)){
            wrapper.ge("gmt_date",startTime);
        }
        if(StringUtils.hasText(endTime)) {
            wrapper.le("gmt_date", endTime);
        }
        IPage<Message> page1 = messageMapper.getMessage(page,wrapper);
        System.out.println("总页码:" + page.getPages());
        System.out.println("总条数:" + page.getTotal());
        System.out.println("当前页记录:" + page.getRecords());
        return new CommonResult(2000,"查询成功",page1);


    }

    @Override
    public CommonResult deleteMessageById(String id) {
        int i = messageMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",i);
    }

    @Override
    public CommonResult getMessageById(Integer id) {
        Message message = messageMapper.selectById(id);
        return new CommonResult(2000,"查询公告成功",message);
    }

    @Override
    public CommonResult updateMessage(Message message) {
        int row = messageMapper.updateById(message);
        return new CommonResult(2000,"修改成功",row);
    }
}
